from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get
from conan.tools.microsoft import is_msvc
import os

required_conan_version = ">=1.53.0"


class Pvmp3decConan(ConanFile):
    name = "pvmp3dec"
    description = "A library and utilities for decoding mp3 files."
    url = "https://github.com/conan-io/conan-center-index"
    license = "apache"
    homepage = "https://android.googlesource.com/platform/frameworks/av/+/android-4.4.4_r1/media/libstagefright/codecs/mp3dec/"
    topics = ("mp3", "audio", "multimedia")
    package_type = "library"
    settings = "os", "arch", "compiler", "build_type"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
    }

    def export_sources(self):
        copy(self, "CMakeLists.txt", os.path.join(self.recipe_folder), self.export_sources_folder)
        export_conandata_patches(self)

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")

    def layout(self):
        cmake_layout(self, src_folder=".")

    def source(self):
        get(self, **self.conan_data["sources"][self.version])

    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()

        if is_msvc(self):
            cd = CMakeDeps(self)
            cd.generate()

    def build(self):
        apply_conandata_patches(self)
        cmake = CMake(self)
        cmake.configure(build_script_folder=os.path.join(self.source_folder))
        cmake.build()

    def package(self):
        copy(self, "MODULE_LICENSE_APACHE2", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
        copy(self, "NOTICE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
        cmake = CMake(self)
        cmake.install()

    def package_info(self):
        self.cpp_info.set_property("cmake_find_mode", "both")
        self.cpp_info.set_property("cmake_file_name", "pvmp3dec")
        self.cpp_info.set_property("cmake_target_name", "pvmp3dec::pvmp3dec")
        self.cpp_info.libs = ["pvmp3dec"]

